package com.qbw.core.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.qbw.log.XLog;

import cn.jpush.android.api.JPushInterface;

/**
 * @author qinbaowei
 * @date 2017/5/17
 * @email qbaowei@qq.com
 */

public class JpushReceiver extends BroadcastReceiver {

    public static final String JFROM = "push";

    @Override
    public void onReceive(Context context, Intent intent) {
        XLog.d("onReceive - " + intent.getAction());
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            XLog.e("bundle == null");
            return;
        }

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            XLog.d("[MyReceiver] 接收Registration Id : " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            XLog.d("收到了自定义消息。消息内容是：" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            // 自定义消息不会展示在通知栏，完全要开发者写代码去处理
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            XLog.d("收到了通知");
            // 在这里可以做些统计，或者做些其他工作
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            XLog.d("用户点击打开了通知");
            // 在这里可以自己写代码去定义用户点击后的行为
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            String data = bundle.getString(JPushInterface.EXTRA_EXTRA);
            XLog.d("title=%s, content=%s, data=%s", title, content, data);

        } else {
            XLog.d("Unhandled intent - " + intent.getAction());
        }
    }

}
